#include <pthread.h>
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#define userNUM 10

int ticketNum = 100;
pthread_mutex_t Lock;

static void *buyTicket(void *args)
{
    std::string tmp = *static_cast<std::string *>(args);

    while (ticketNum > 0)
    {
        pthread_mutex_lock(&Lock);
        std::cout << "当前票数:" << ticketNum << tmp << "   正在买票中" << std::endl;
        --ticketNum;
        pthread_mutex_unlock(&Lock);
    }
}
int main()
{
    std::vector<pthread_t> users;

    users.reserve(userNUM);
    for (int i = 0; i < userNUM; ++i)
    {
        std::string inf = "用户";
        inf += (char)(i + '0');
        int n = pthread_create(&users[i], nullptr, buyTicket, (void *)&inf);
        assert(n == 0);
        (void)n;
    }

    for (int i = 0; i < userNUM; ++i)
    {
        pthread_join(users[i], nullptr);
    }

    return 0;
}